import { Component, OnInit, ViewChild, Input } from "@angular/core";
import "datatables";
import { ModalDirective } from "ng2-bootstrap/ng2-bootstrap";
import { CertificateManService } from "./certificate-management.service";
import { DataTablesComponent, DataTablesComponentConfig } from "app/theme/components/dataTables";
import { CertManInfo } from "./certificate-management-info.class";

@Component({
  selector: 'certificate-management',
  templateUrl: 'certificate-management.component.html',
  styleUrls: ['certificate-management.component.scss'],
  providers: [CertificateManService]
})
export class CertificateManComponent implements OnInit {
  @Input() type: string;

  @ViewChild('applyModal') applyModal: ModalDirective;
  @ViewChild(DataTablesComponent) dataTable: DataTablesComponent;

  private applyUrl: string = '/water/certificate/showCerertificate';
  private applyTableUrl: string = '/water/certificate/showAllCertifi';
  private apprDownloadUrl = '/water/certificateSub/printCertificate';
  private downloadUrl: string = '';
  private searchForm: CertManSearchForm = new CertManSearchForm();

  private disableViewBtn = true;

  private selectedRowData: any = {};
  private selectedData: CertManInfo = new CertManInfo();
  private _errorMsg: string;
  private errorShow = false;

  private dataTablesConfig: DataTablesComponentConfig = new DataTablesComponentConfig();

  constructor(private _service: CertificateManService) {
  }

  ngOnInit() {
    this.initDatatables();
  }

  private initDatatables() {
    this.dataTablesConfig.ajaxUrl = this.applyTableUrl;
    this.dataTablesConfig.selectable = 'single';
    this.dataTablesConfig.columnsDef = [
      {'name': '证书类型', 'id': 'type'},
      {'name': '证书编号', 'id': 'serialno'},
      {'name': '姓名', 'id': 'name'},
      {'name': '公司名', 'id': 'companyname'},
      {'name': '签发日期', 'id': 'issuedate','orderable': true},
      {'name': '有效期', 'id': 'availabletime','orderable': true},
      {'name': '状态', 'id': 'available'}
    ];
  }

  private loadCertInfo(ids: string) {
    this.selectedData = new CertManInfo();
    this._service.loadCertInfo(this.applyUrl, ids)
      .then(res => {
        if (res.status === 'success') {
          this.selectedData = res.data;
          this.applyModal.show();
        } else {
          this._errorMsg = res.data ? res.data : '网络错误';
        }
      }, error => {
        this._errorMsg = error;
      });
  }

  handleError(e:any){
    if(e.type == 'error'){
      this.errorShow = true;
    }
  }

  private toggleView(): void {
    let ids = this.selectedRowData.ids;
    this.loadCertInfo(ids);
  }

  search() {
    this.dataTable.draw();
  }

  onHideModal(): void {
    jQuery(".modal-backdrop").remove();
  }

  tableRowSelect(rowData: any[]) {
    if (rowData.length === 1) {
      this.selectedRowData = rowData[0];
      this.disableViewBtn = false;
      this.downloadUrl = this.apprDownloadUrl + '?ids=' + rowData[0].ids;
    } else {
      this.disableViewBtn = true;
    }
  }

}

export class CertManSearchForm {
  public idcard: string = '';
  public name: string = '';
  public type: string = '';
  public available: string = '0';
  public serialno: string = '';
}
